package com.springboot2.security.controller;

import com.springboot2.security.entity.po.ResponseEntity;
import com.springboot2.security.entity.po.UserInfo;
import com.springboot2.security.mappers.user.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class IndexController {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserDao userRepository;

//    @Autowired
//    private AuthenticationManager myAuthenticationManager;

    @Autowired
    private UserDao userDao;

    @ResponseBody
    @RequestMapping("/personal_center")
    public void login(HttpServletRequest request) {
        System.out.println("登录成功");
    }

    @ResponseBody
    @PostMapping("/registry")
    public void registry(UserInfo user) {
        userRepository.insert(new UserInfo(user.getUserName(), passwordEncoder.encode(user.getPassword())));
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @ResponseBody
    @GetMapping("/testRole")
    public void testRole() {
        System.out.println("324324");
    }

    @ResponseBody
    @GetMapping("/testNone")
    public void testNone() {
        System.out.println("324324");
    }


    @PreAuthorize("hasRole('USER')")
    @ResponseBody
    @GetMapping("/getUsers")
    public ResponseEntity getUsers() {
        System.out.println("324324");
        return ResponseEntity.success();
    }
}